﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data.SQLite;
using Com.Toook.Common;

public partial class nst_param_UserVisitRecord : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            InitVisit();
        }
    }

    private void InitVisit()
    {
        int startId, endId;
        string condition = "";
        if (!string.IsNullOrEmpty(txtStartId.Text) 
            && !string.IsNullOrEmpty(txtEndId.Text)
            && int.TryParse(txtEndId.Text, out endId)
            && int.TryParse(txtStartId.Text, out startId))
        {
            condition = " where id>=" + startId.ToString() + " and id<=" + endId.ToString() + " ";
        }
        string sql = "select id,ip,stay,creatTime,agent,url,refer from UserVisitRecord " + condition + " limit 0," + ddlShowCount.SelectedValue;

        SQLiteConnection con = SqliteHelper.GetConnection("SQLiteDatabase");
        SQLiteCommand cmd = new SQLiteCommand(sql, con);
        SQLiteDataReader reader = null;
        try
        {
            con.Open();
            reader = cmd.ExecuteReader();
            GridViewVisit.DataSource = reader;
            GridViewVisit.DataBind();
        }
        catch (Exception ex)
        {
            LogHelper.Log.Error(ex);
        }
        finally
        {
            SqliteHelper.CloseDbResource(con, reader);
        }
    }
    protected void ButtonView_Click(object sender, EventArgs e)
    {
        InitVisit();
    }
    protected void ButtonDelete_Click(object sender, EventArgs e)
    {
        int startId = 0, endId = 0;
        if (string.IsNullOrEmpty(txtStartId.Text)
           && string.IsNullOrEmpty(txtEndId.Text)
           && !int.TryParse(txtEndId.Text, out endId)
           && !int.TryParse(txtStartId.Text, out startId))        
        {
            return;
        }
        string sql = "delete from UserVisitRecord  where id>=" + startId.ToString() + " and id<=" + endId.ToString() ;

        SQLiteConnection con = SqliteHelper.GetConnection("SQLiteDatabase");
        SQLiteCommand cmd = new SQLiteCommand(sql, con);

        bool success = true;
        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
            lblMsg.Text = "删除成功";
            txtEndId.Text = txtStartId.Text = "";
        }
        catch (Exception ex)
        {
            LogHelper.Log.Error(ex);
            lblMsg.Text = "删除失败";
            success = false;
        }
        finally
        {
            SqliteHelper.CloseDbResource(con, null);
        }

        if (success)
        {
            InitVisit();
        }
    }
}
